﻿using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.IRepository.BusinessManage;
using NFine.Repository.BusinessManage;
using NFine.Code;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System;
using NFine.Domain.Entity.SystemManage;

namespace NFine.Application.BusinessManage
{
    public class D_IndentApp
    {
        private ID_IndentRepository service = new D_IndentRepository();

        public List<D_IndentEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<D_IndentEntity>();
            string loginid = OperatorProvider.Provider.GetCurrent().UserId;
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Name.Contains(keyword));
            }
            expression = expression.And(t => t.D_M_F_ID == loginid);
            return service.FindList(expression, pagination);
        }

        public List<D_IndentEntity> GetIndentGridJson(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<D_IndentEntity>();
            string loginid = OperatorProvider.Provider.GetCurrent().UserId;
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Name.Contains(keyword));
            }
            expression = expression.And(t => t.D_M_F_ID == loginid);
            return service.GetIndentGridJson(expression, pagination);
        }

        public List<D_IndentEntity> GetMemList(Pagination pagination, string keyword)
        {
            var model= new D_IndentEntity();
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                model = JsonConvert.DeserializeObject<D_IndentEntity>(keyword);
            }
            var expression = ExtLinq.True<D_IndentEntity>();            
            if (!string.IsNullOrEmpty(model.F_Name))
            {
                expression = expression.And(t => t.F_Name.Contains(model.F_Name));
            }
            if (model.F_EnabledMark!=null)
            {
                expression = expression.And(t => t.F_EnabledMark==model.F_EnabledMark);
            }
            else
                expression = expression.And(t => t.F_EnabledMark == false);
            var expression1 = ExtLinq.True<UserEntity>();
            if (!string.IsNullOrEmpty(model.F_MemAccount))
            {
                expression1 = expression1.And(t => t.F_Account.Contains(model.F_MemAccount));
            }
            if (!string.IsNullOrEmpty(model.F_MemName))
            {
                expression1 = expression1.And(t => t.F_RealName.Contains(model.F_MemName));
            }
            return service.FindMenList(expression, expression1, pagination);
        }

        public List<D_IndentEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public D_IndentEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public D_IndentEntity GetMemberIndentFormJson(string keyValue)
        {
            return service.GetMemberIndentFormJson(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(D_IndentEntity indentEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                indentEntity.Modify(keyValue);
                service.Update(indentEntity);
            }
            else
            {
                indentEntity.Create();
                service.Insert(indentEntity);
            }
        }
        public void CheckForm(D_IndentEntity indentEntity, string keyValue)
        {
            indentEntity.Modify(keyValue);
            service.CheckForm(indentEntity, keyValue);
        }
    }
}
